/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package ot.s3;

import java.io.*;
import java.lang.reflect.*;
import ot.*;

/**
 *
 * @author Barnet Wagman
 */
public class UtilOtS3 {

    static String throwableToString(Throwable x) {
        
        if ( x == null ) return("NULL exception!");
        
        String m = x.getMessage();
        String s = x.toString();
        
        String mess = "";
        if ( m != null ) mess += m + "\n";
        if ( s != null ) mess += s + "\n";
        
        mess += "\n" + stackTraceToString(x.getStackTrace());
        Throwable cause = x.getCause();
        if ( cause != null ) {
            mess += "\nCAUSE:\n" + throwableToString(cause);
        }
        return(mess);
    }
    
    static String stackTraceToString(StackTraceElement[] trace) {
        if ( trace == null ) return("NULL stack trace.");
        String s = "";
        for ( int i = 0; i < trace.length; i++ ) {
            if ( trace[i] != null )
                s += trace[i].toString() + "\n";
            else s += "null trace element at [" + i + "]";
        }
        return(s);
    }  
    
    static void nap(long millis) {
        try {
            Thread.sleep(millis);
        }
        catch(InterruptedException ix) {}
    }
    
    public static int sizeInBytes(Object x) throws OtException {
        if ( x == null ) return(0);
        else return( objectToByteArray(x).length );
    }
    
    public static byte[] objectToByteArray(Object obj ) throws OtException {

        ByteArrayOutputStream baos = null;        
        ObjectOutputStream oos = null;
        try {                               
	    baos = new ByteArrayOutputStream();	 
	    oos = new ObjectOutputStream(baos);
	    oos.writeObject(obj);            
	    // oos.flush();      
            oos.reset();
            byte[] b = baos.toByteArray();                                    
            return(b);           
	}        
	catch (java.io.IOException iox) { throw new OtException(iox); }
        finally {
            try {
                if ( oos != null ) { oos.close(); oos = null; }               
                if ( baos != null ) { baos.close(); baos = null; }
                /* ^^^^^^^^ WARNING-Java BUG! when serializing large objects
                 * the baos seems to leak memory.  When this method returns
                 * baos is unreferenced and SHOULD get all its space freed up,
                 * but it does not appear to, unless we explicitly null it out.
                 */
            }
            catch(IOException iox) { throw new OtException(iox); }
        }
    } 
    
        /** Gets the fully qualified method name of the current method.
         *  @return
         */
    public static String getCurrentMethodNameFullyQualified() {
        
        StackTraceElement[] sEls = Thread.currentThread().getStackTrace();        
        
        if ( (sEls != null) && (sEls.length >= 1) ) {            
            return( sEls[2].getClassName() + "." + sEls[2].getMethodName() );
        }        
        else return(null);
    }
    
        /** Gets the fully qualified method name of method that
         *  called the current method, or null if the current method
         *  was called by <tt>main()</tt>
         * @return
         */
    public static String getParentMethodNameFullyQualified() {
        
        StackTraceElement[] sEls = Thread.currentThread().getStackTrace();        
        
        if ( (sEls != null) && (sEls.length >= 2) ) {            
            return( sEls[3].getClassName() + "." + sEls[3].getMethodName() );
        }        
        else return(null);
    }
}
